<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @subpackage Demos
 * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * PHP sample code for the Google Calendar data API.  Utilizes the
 * Zend Framework Gdata components to communicate with the Google API.
 *
 * Requires the Zend Framework Gdata components and PHP >= 5.2.11
 *
 * You can run this sample both from the command line (CLI) and also
 * from a web browser.  When running through a web browser, only
 * AuthSub and outputting a list of calendars is demonstrated.  When
 * running via CLI, all functionality except AuthSub is available and dependent
 * upon the command line options passed.  Run this script without any
 * command line options to see usage, eg:
 *     /usr/local/bin/php -f Calendar.php
 *
 * More information on the Command Line Interface is available at:
 *     http://www.php.net/features.commandline
 *
 * NOTE: You must ensure that the Zend Framework is in your PHP include
 * path.  You can do this via php.ini settings, or by modifying the
 * argument to set_include_path in the code below.
 *
 * NOTE: As this is sample code, not all of the functions do full error
 * handling.  Please see getEvent for an example of how errors could
 * be handled and the online code samples for additional information.
 */

/**
 * @see Zend_Loader
 */
require_once 'Zend/Loader.php';

/**
 * @see Zend_Gdata
 */
Zend_Loader::loadClass('Zend_Gdata');

/**
 * @see Zend_Gdata_AuthSub
 */
Zend_Loader::loadClass('Zend_Gdata_AuthSub');

/**
 * @see Zend_Gdata_ClientLogin
 */
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

/**
 * @see Zend_Gdata_HttpClient
 */
Zend_Loader::loadClass('Zend_Gdata_HttpClient');

/**
 * @see Zend_Gdata_Calendar
 */
Zend_Loader::loadClass('Zend_Gdata_Calendar');

/**
 * @var string Location of AuthSub key file.  include_path is used to find this
 */
$_authSubKeyFile = null; // Example value for secure use: 'mykey.pem'

/**
 * @var string Passphrase for AuthSub key file.
 */
$_authSubKeyFilePassphrase = null;

/**
 * Returns the full URL of the current page, based upon env variables
 *
 * Env variables used:
 * $_SERVER['HTTPS'] = (on|off|)
 * $_SERVER['HTTP_HOST'] = value of the Host: header
 * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
 * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
 *
 * @return string Current URL
 */
//$startDate = $this->_getParam('start');
//$startDate =  $_GET["start"];
   // $startDate = date("Y-m-d", $startDate);
    //$endDate = $this->_getParam('end');
  //   $endDate = $_GET["end"];
   // $endDate = date("Y-m-d", $endDate);
    // Remove the view & layout
   // $this->_helper->layout->disableLayout();
    //$this->_helper->viewRenderer->setNoRender(true);
    // Query Google GData for the calendar
    $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $source = "csl-test";
    //$source = "test google calendar";
    $user = "isbas.thecloud@gmail.com";
    $pass = "google1234567899";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service,null,$source);
   // echo $client;
	$cal = new Zend_Gdata_Calendar($client);
   try {
	$event= $cal->newEventEntry();
 
// Populate the event with the desired information
// Note that each attribute is crated as an instance of a matching class
$event->title = $cal->newTitle($_POST['eventTitle']);
$event->where = array($cal->newWhere("Palo Alto, California"));
$event->content = $cal->newContent('this is great ');
 
// Set the duration and frequency by specifying a recurrence pattern.
 
//$recurrence = "DTSTART;VALUE=DATE:20070501\r\n" .
//        "DTEND;VALUE=DATE:20070502\r\n" .
//        "RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20070904\r\n";
// 
//$event->recurrence = $cal->newRecurrence($recurrence);
 
// Upload the event to the calendar server
// A copy of the event as it is recorded on the server is returned
	echo substr($_POST['startDate'],-8,5);
	$startDate = substr($_POST['startDate'],0,-9); $startTime = substr($_POST['startDate'],-8,5);
    $endDate = substr($_POST['endDate'],0,-9); $endTime = substr($_POST['endDate'],-8,5); $tzOffset = '+07';
// $startDate = '2014-07-25'; $startTime = '10:00';
//    $endDate = '2014-07-25'; $endTime = '11:00'; $tzOffset = '+07';

$when = $cal->newWhen(); 
      $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00"; 
      $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00"; 
      $event->when = array($when); 
$newEvent = $cal->insertEvent($event);
} catch (Zend_Gdata_App_Exception $e) {
			echo "Error: " . $e->getResponse();
		}
		echo 'Event successfully added!'; 
	?>